package create

import (
	"context"
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
)

type Obj struct {
	Id   int32
	Text string
}

// 有常见的类型
type BaseAdd struct {
	Id         int
	Int32      int32
	Int64      int64
	Float32    float32
	Float64    float64
	Text       string
	Boolean    bool
	ByteList   []byte
	StringList []string
	IntList    []int32
	Date       time.Time
	Object     Obj
	ObjectID   primitive.ObjectID
}

// 最基本的新增一条数据
func (a *BaseAdd) Add(mongo *mongo.Database, ctx context.Context) error {
	result, err := mongo.Collection("test").InsertOne(ctx, a)
	fmt.Println(result.InsertedID) // 会返回主键_id的值
	return err

}

// 插入多条数据
func AddMany(mongo *mongo.Database, ctx context.Context, data ...interface{}) {
	result, err := mongo.Collection("test").InsertMany(ctx, data)
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(result.InsertedIDs...) //返回的是主键数组
}
